package api

import (
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/grpc_client"
	pb "longmen/server/pkg/protobuf"

	"github.com/labstack/echo/v4"
	"longmen/server/pkg/common/api_models"
)

// GetFootballOddsHistory
// @Summary 赛事详情 - 指数详情
// @Description 历史指数, 公司ID 不传即为查所有公司的
// @Accept  json
// @Produce  json
// @Tags 指数
// @Param   sendBody body api_models.OddsHistoryRequest true "body"
// @Success 200 {object} protobuf.OddsItem "result"
// @Router /api/odds/football_history [POST]
func (base *Controller) GetFootballOddsHistory(c echo.Context) error {
	req := api_models.OddsHistoryRequest{}
	err := base.BindAndValid(c, &req)
	if err != nil {
		return util.BadParam(c, err)
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	odds, err := grpc_client.GrpcSportServer.FootBallHistoryOdds(ctx, &pb.HistoryOddsRequest{
		Match: &pb.MatchRequest{
			MatchId: uint32(req.MatchId),
			ExType:  req.ExType,
		},
		CompanyId: int32(req.CompanyId),
	})

	if err != nil {
		return util.BadParam(c, err)
	}

	return util.BuildSuccess(c, odds.Items, "")
}

// GetFootballInitialOdds
// @Summary 赛事详情 - 指数
// @Description 查询初盘数据
// @Accept  json
// @Produce  json
// @Tags 指数
// @Param   sendBody body api_models.InitialOddsRequest true "body"
// @Success 200 {object} protobuf.FootballInitOddsResp "result"
// @Router /api/odds/football_initial [POST]
func (base *Controller) GetFootballInitialOdds(c echo.Context) error {
	req := api_models.InitialOddsRequest{}
	err := base.BindAndValid(c, &req)
	if err != nil {
		return util.BadParam(c, err)
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	odds, err := grpc_client.GrpcSportServer.FootBallInitialOdds(ctx, &pb.MatchRequest{
		MatchId: uint32(req.MatchId),
		ExType:  req.ExType,
	})

	if err != nil {
		return util.BadParam(c, err)
	}

	return util.BuildSuccess(c, odds, "")
}

// GetBasketBallOddsHistory
// @Summary 赛事详情 - 指数详情
// @Description 历史指数, 公司ID 不传即为查所有公司的
// @Accept  json
// @Produce  json
// @Tags 指数
// @Param   sendBody body api_models.OddsHistoryRequest true "body"
// @Success 200 {object} protobuf.OddsItem "result"
// @Router /api/odds/basketball_history [POST]
func (base *Controller) GetBasketBallOddsHistory(c echo.Context) error {
	req := api_models.OddsHistoryRequest{}
	err := base.BindAndValid(c, &req)
	if err != nil {
		return util.BadParam(c, err)
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	odds, err := grpc_client.GrpcSportServer.BasketBallHistoryOdds(ctx, &pb.HistoryOddsRequest{
		Match: &pb.MatchRequest{
			MatchId: uint32(req.MatchId),
			ExType:  req.ExType,
		},
		CompanyId: int32(req.CompanyId),
	})

	if err != nil {
		return util.BadParam(c, err)
	}

	return util.BuildSuccess(c, odds.Items, "")
}

// GetBasketBallInitialOdds
// @Summary 赛事详情 - 指数
// @Description 查询初盘数据
// @Accept  json
// @Produce  json
// @Tags 指数
// @Param   sendBody body api_models.InitialOddsRequest true "body"
// @Success 200 {object} protobuf.BasketBallInitOddsResp "result"
// @Router /api/odds/basketball_initial [POST]
func (base *Controller) GetBasketBallInitialOdds(c echo.Context) error {
	req := api_models.InitialOddsRequest{}
	err := base.BindAndValid(c, &req)
	if err != nil {
		return util.BadParam(c, err)
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	odds, err := grpc_client.GrpcSportServer.BasketBallInitialOdds(ctx, &pb.MatchRequest{
		MatchId: uint32(req.MatchId),
		ExType:  req.ExType,
	})

	if err != nil {
		return util.BadParam(c, err)
	}

	return util.BuildSuccess(c, odds, "")
}
